package com.siwei.mes.common.base;

import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Column;
import java.util.Date;

/**
 * @author chenmaolin
 * @version 1.0
 * @date 2022/10/29
 * @Description 通用实体类model
 */
@Data
public class BaseEntity {
    /**
     * 状态 1-正常
     */
    @Column(name = "state")
    @ApiModelProperty(value = "状态 1-正常", name = "state")
    private Integer state;
    /**
     * 创建人
     */
    @Column(name = "creater")
    @ApiModelProperty(value = "创建人", name = "creater")
    private Long creater;
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @ApiModelProperty(value = "创建时间", name = "createTime")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;
    /**
     * 修改人
     */
    @Column(name = "updater")
    @ApiModelProperty(value = "修改人", name = "updater")
    private Long updater;
    /**
     * 更新时间
     */
    @Column(name = "update_time")
    @ApiModelProperty(value = "更新时间", name = "updateTime")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;

    /**
     * 是否删除
     */
    @Column(name = "is_del")
    @ApiModelProperty(value = "是否删除", name = "isDel")
    private Integer isDel;


    /**
     * 是否新增
     *
     * @param flag true-新增  false-修改
     */
    public void init(Boolean flag) {
        long userId = StpUtil.getLoginIdAsLong();
        if (flag) {
            this.creater = userId;
            this.createTime = new Date();
            this.state = 1;
            this.isDel = 0;
        } else {
            this.updater = userId;
            this.updateTime = new Date();
        }
    }

}
